package com.hg.library.graph;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * Created by wangjianqiang on 2016/5/27.
 */
public class ImageGetHelper {
    private static final String CACHE_PATH = "/mnt/internal_sd/Android/data/";

    public static void setBackGroundDrawable(final View root, final Context context, final int resourseID) {
        if(setDrawableAsncTask != null && setDrawableAsncTask.getStatus() == AsyncTask.Status.RUNNING){
            setDrawableAsncTask.cancel(true);
        }
        setDrawableAsncTask =  new MyAsncTask(root, context, resourseID);
        setDrawableAsncTask.execute();
    }

    private static class MyAsncTask extends AsyncTask<String, Integer, Bitmap> {
        private Context context;
        private int resourseID;
        private View view;

        @Override
        protected Bitmap doInBackground(String... params) {
            String path = CACHE_PATH +context.getBasePackageName()+"/cache/imageCache/" + resourseID + ".jpg";
            Bitmap bitmap = null;
            File bitmapFile = new File(path);
            if(bitmapFile != null && bitmapFile.exists()){
                bitmap = ImageLoader.getInstance().loadImageSync("file:///"+path);
            }
            if (bitmap == null) {
                bitmap = BitmapFactory.decodeResource(context.getResources(), resourseID);
                bitmap = Bitmap.createBitmap(bitmap, 20, 20, bitmap.getWidth() - 20, bitmap.getHeight() / 2);
                Bitmap bitmap1 = ImageProcess.Blur(bitmap, 50);
                File parentFile = new File(CACHE_PATH +context.getBasePackageName()+"/cache/imageCache/");
                if (!parentFile.exists()) {
                    parentFile.mkdirs();
                }
                File file = new File(path);
                if (file.exists()) {
                    file.delete();
                }
                try {
                    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                    bitmap1.compress(Bitmap.CompressFormat.PNG, 100, out);
                    out.flush();
                    out.close();
                } catch (Exception e) {

                }
                bitmap.recycle();
                bitmap = bitmap1;
            }
            return bitmap;
        }
        public MyAsncTask(View view, Context context, int resourseID) {
            super();
            this.view = view;
            this.context = context;
            this.resourseID = resourseID;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            if(bitmap != null){
                view.setBackground(new BitmapDrawable(bitmap));
            }
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    }
    private static MyAsncTask setDrawableAsncTask = null;
}
